package com.yunchang.springboot.utils.mwsutils;

import com.yunchang.springboot.mapper.mwsreport.MwsPaymentMapper;
import com.yunchang.springboot.mapper.mwsutils.CwfzMarketplaceCountryMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class GetCountryByMarketplaceNameOrSettlementIdUtil {

    // payment
    private static  MwsPaymentMapper mwsPaymentMapper;

    @Autowired
    public void setMwsPaymentMapper(MwsPaymentMapper mwsPaymentMapper) {
        GetCountryByMarketplaceNameOrSettlementIdUtil.mwsPaymentMapper = mwsPaymentMapper;
    }

    // 用于查询countryName的类
    private static CwfzMarketplaceCountryMapper cwfzMarketplaceCountryMapper;

    @Autowired
    public void setCwfzMarketplaceCountryMapper(CwfzMarketplaceCountryMapper cwfzMarketplaceCountryMapper) {
        GetCountryByMarketplaceNameOrSettlementIdUtil.cwfzMarketplaceCountryMapper = cwfzMarketplaceCountryMapper;
    }

    /**
     * 根据marketplace或者settlementId获取country
     *
     * @param marketplaceName
     * @param settlementId
     * @return
     */
    public static String getCountryByMarketplaceNameOrSettlementId(String marketplaceName, String settlementId) {
        String country = null;
        if (StringUtils.isEmpty(marketplaceName)) {
            if (!StringUtils.isEmpty(settlementId)) {
                List<String> marketplaceNameList = mwsPaymentMapper.selectMarketplaceNameBySettlementId(settlementId);
                if (null != marketplaceNameList && !marketplaceNameList.isEmpty()) {
                    for (String marketplaceNameFor : marketplaceNameList) {
                        if (marketplaceNameFor.startsWith("Amazon")) {
                            marketplaceName = marketplaceNameFor;
                        }
                    }
                } else {
                    log.warn("根据settlementId获取marketplaceName得到的集合为空,payment其他费用");
                }
            } else {
                log.warn("获取payment其他费用数据中的settlementId为空,payment其他费用");
            }
            if (!StringUtils.isEmpty(marketplaceName)) {
                country = cwfzMarketplaceCountryMapper.selectCountryByMarketplace(marketplaceName);
            }
        } else {
            country = cwfzMarketplaceCountryMapper.selectCountryByMarketplace(marketplaceName);
        }
        return country;
    }

}
